package club.dataresources.service.impl;

import club.dataresources.entity.Waiter;
import club.dataresources.entity.WaiterExample;
import club.dataresources.mapper.WaiterMapper;
import club.dataresources.service.WaiterService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author
 * @create 2018-11-11 10:17
 **/
@Service
public class WaiterServiceImpl implements WaiterService{
    @Autowired
    private WaiterMapper waiterMapper;

    @Override
    public boolean addWaiter(Waiter waiter) throws Exception {
        return waiterMapper.insertSelective(waiter)==1;
    }

    @Override
    public boolean removeWaiter(Integer waiterId) throws Exception {
        return waiterMapper.deleteByPrimaryKey(waiterId)==1;
    }

    @Override
    public boolean updateWaiter(Waiter waiter) throws Exception {
        return waiterMapper.updateByPrimaryKeySelective(waiter)==1;
    }

    @Override
    public Page<Waiter> listWaiter(Integer page, Integer rows, Waiter waiter) throws Exception {
        PageHelper.startPage(page, rows);
        WaiterExample example = new WaiterExample();
        WaiterExample.Criteria criteria = example.createCriteria();

        //如果是模糊查询
        if(waiter.getWaiterName()!=null){
            criteria.andWaiterNameLike(waiter.getWaiterName());
        }
        criteria.andWaiterIdIsNotNull();
        return waiterMapper.selectByExample(example);
    }

    @Override
    public Waiter getRandomWaiter() throws Exception {
        return waiterMapper.getRandomWaiter();
    }
}
